package com.unidt.www.framework.mybatis.core.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;

import java.lang.invoke.SerializedLambda;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * @Author zjk
 * @Description:
 * @Date 2021/12/1 15:26
 **/
public class ColumnUtil {

    /**
     * 功能描述: 获取对象属性方法名
     *
     * @param fn
     * @return java.lang.String
     * @author zkj
     * @date 2021/12/1
     */

    public static <T> String getName(SFunction<T, ?> fn) {
        // 从function取出序列化方法
        Method writeReplaceMethod;
        try {
            writeReplaceMethod = fn.getClass().getDeclaredMethod("writeReplace");
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

        // 从序列化方法取出序列化的lambda信息
        boolean isAccessible = writeReplaceMethod.isAccessible();
        writeReplaceMethod.setAccessible(true);
        SerializedLambda serializedLambda;
        try {
            serializedLambda = (SerializedLambda) writeReplaceMethod.invoke(fn);
        } catch (IllegalAccessException | InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        writeReplaceMethod.setAccessible(isAccessible);

        // 从lambda信息取出method、field、class等
        String fieldName = serializedLambda.getImplMethodName().substring("get".length());
        fieldName = fieldName.replaceFirst(fieldName.charAt(0) + "", (fieldName.charAt(0) + "").toLowerCase());

        return fieldName;
    }


    /**
     * 功能描述: 将字符串类型格式未xx,xx 转换成treeSet
     *
     * @param text
     * @return java.util.Set<java.lang.String>
     * @author zkj
     * @date 2021/12/23
     */

    public static Set<Object> strToSet(String text) {
        Set<Object> treeSet = new HashSet<>();

        if (StrUtil.isNotBlank(text)) {
            String[] array = text.split("[,]");
            for (int i = 0; i < array.length; i++) {
                String t = array[i];
                treeSet.add(t);
            }
        }
        return treeSet;
    }

    /**
     * 功能描述: 将treeSet 转换成 字符串类型格式未xx,xx
     *
     * @param set
     * @return java.lang.String
     * @author zkj
     * @date 2021/12/23
     */
    public static String setToStr(Set<Object> set) {

        StringBuilder stringBuilder = new StringBuilder();

        Boolean begin = true;

        for (Object text : set) {
            if (begin) {
                stringBuilder.append(text);
                begin = false;
            } else {
                stringBuilder.append("," + text);

            }
        }
        return stringBuilder.toString();
    }


    public static void main(String[] args) {

        String text = "1,2,4,5";
        Set<Object> s = strToSet(text);

        System.out.println(s.toString());

        s.add(String.valueOf(1));

        s.add(String.valueOf(7));

        String t1 = setToStr(s);

        System.out.println(t1);


    }
}
